﻿<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:l="http://wpflocalizeextension.codeplex.com"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Imagin.Controls.Common"
    xmlns:Controls.Common.Extensions="clr-namespace:Imagin.Controls.Common.Extensions"
    xmlns:Controls.Common.Converters="clr-namespace:Imagin.Controls.Common.Converters">
    <Controls.Common.Converters:WindowTypeVisibilityConverter x:Key="WindowTypeVisibilityConverter"/>

    <DataTemplate x:Key="DataTemplate.Window.Icon">
        <Grid Margin="10,0" VerticalAlignment="Center">
            <local:MaskedImage Source="{Binding Mode=OneWay}"/>
        </Grid>
    </DataTemplate>
    
    <DataTemplate x:Key="DataTemplate.Window.ResizeGrip">
        <Path 
            Data="F1 M1,10 L3,10 M5,10 L7,10 M9,10 L11,10 M2,9 L2,11 M6,9 L6,11 M10,9 L10,11 M5,6 L7,6 M9,6 L11,6 M6,5 L6,7 M10,5 L10,7 M9,2 L11,2 M10,1 L10,3"
            Height="12" 
            HorizontalAlignment="Right" 
            Margin="0,0,4,4" 
            Stroke="{Binding Mode=OneWay}" 
            StrokeThickness="1" 
            Stretch="None" 
            VerticalAlignment="Bottom"
            Width="12"/>
    </DataTemplate>
    
    <DataTemplate x:Key="DataTemplate.Window.Title">
        <TextBlock 
            x:Name="titleTextBlock" 
            FontSize="13"
            Foreground="{DynamicResource Brush.Text}" 
            HorizontalAlignment="Left" 
            Margin="0,-1,0,0" 
            Text="{Binding Mode=OneWay}" 
            TextTrimming="CharacterEllipsis" 
            VerticalAlignment="Center"/>
    </DataTemplate>

    <ItemsPanelTemplate x:Key="ItemsPanelTemplate.Window.Buttons">
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>

    <ControlTemplate x:Key="Template.BasicWindow" TargetType="{x:Type local:BasicWindow}">
        <Border 
            x:Name="PART_Border" 
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            Margin="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}}">
            <Grid x:Name="LayoutRoot">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                
                <Grid Height="32">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <ContentPresenter
                        Content="{Binding Path=(Controls.Common.Extensions:WindowExtensions.Icon), RelativeSource={RelativeSource TemplatedParent}}"
                        ContentTemplate="{TemplateBinding IconTemplate}"/>
                    
                    <ContentPresenter
                        Grid.Column="1" 
                        x:Name="PART_TitleGrid" 
                        Content="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:BasicWindow}}}"
                        ContentTemplate="{TemplateBinding TitleTemplate}"/>
                    
                    <ItemsControl 
                        Grid.Column="3" 
                        WindowChrome.IsHitTestVisibleInChrome="True"
                        ItemsPanel="{TemplateBinding ButtonsPanel}"
                        ItemsSource="{TemplateBinding Buttons}" 
                        HorizontalAlignment="Right" 
                        VerticalAlignment="Center">
                    </ItemsControl>
                    
                    <StackPanel
                        Grid.Column="4" 
                        Orientation="Horizontal" 
                        VerticalAlignment="Top"
                        WindowChrome.IsHitTestVisibleInChrome="True">
                        <Button 
                            x:Name="PART_MinimizeButton"
                            Command="{Binding MinimizeCommand, RelativeSource={RelativeSource TemplatedParent}}"
                            Content="pack://application:,,,/Imagin.Controls.Common;component/Images/Minus.png"
                            Style="{TemplateBinding ButtonStyle}"
                            ToolTip="{l:Loc Minimize}"/>
                        
                        <Grid Visibility="{TemplateBinding Type, Converter={StaticResource WindowTypeVisibilityConverter}}">
                            <Button 
                                x:Name="PART_RestoreButton" 
                                Command="{Binding RestoreCommand, RelativeSource={RelativeSource TemplatedParent}}"
                                Content="pack://application:,,,/Imagin.Controls.Common;component/Images/Restore.png"
                                Style="{TemplateBinding ButtonStyle}"
                                ToolTip="{l:Loc Restore}"/>
                            <Button 
                                x:Name="PART_MaximizeButton" 
                                Command="{Binding MaximizeCommand, RelativeSource={RelativeSource TemplatedParent}}"
                                Content="pack://application:,,,/Imagin.Controls.Common;component/Images/Maximize.png"
                                Style="{TemplateBinding ButtonStyle}"
                                ToolTip="{l:Loc Maximize}"/>
                        </Grid>
                        
                        <Button 
                            x:Name="PART_CloseButton"
                            Command="{Binding BeginCloseCommand, RelativeSource={RelativeSource TemplatedParent}}"
                            Content="pack://application:,,,/Imagin.Controls.Common;component/Images/X.png"
                            Style="{TemplateBinding ButtonStyle}"
                            ToolTip="{l:Loc Close}"/>
                    </StackPanel>
                </Grid>
                
                <AdornerDecorator Grid.Row="1" >
                    <Border 
                        x:Name="PART_ContentBorder" 
                        BorderBrush="{TemplateBinding ContentBorderBrush}"
                        BorderThickness="{TemplateBinding ContentBorderThickness}"
                        Margin="{TemplateBinding ContentMargin}"
                        Padding="{TemplateBinding Padding}">
                        <ContentPresenter 
                            x:Name="PART_Content"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"/>
                    </Border>
                </AdornerDecorator>

                <ContentPresenter
                    Grid.Row="2" 
                    x:Name="PART_Footer" 
                    Content="{TemplateBinding Footer}"/>

                <ContentPresenter
                    Grid.RowSpan="3" 
                    x:Name="PART_ResizeGrip" 
                    Content="{TemplateBinding Foreground}"
                    ContentTemplate="{TemplateBinding ResizeGripTemplate}"
                    Visibility="Collapsed"/>

                <ContentPresenter
                    Grid.RowSpan="3" 
                    x:Name="PART_Overlay" 
                    Content="{TemplateBinding Overlay}"
                    Visibility="{TemplateBinding OverlayVisibility}"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="WindowState" Value="Maximized">
                <Setter TargetName="PART_MaximizeButton" Property="Visibility" Value="Collapsed" />
                <Setter TargetName="PART_RestoreButton" Property="Visibility" Value="Visible" />
            </Trigger>
            <Trigger Property="WindowState" Value="Normal">
                <Setter TargetName="PART_MaximizeButton" Property="Visibility" Value="Visible" />
                <Setter TargetName="PART_RestoreButton" Property="Visibility" Value="Collapsed" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode" Value="CanResizeWithGrip" />
                    <Condition Property="WindowState" Value="Normal" />
                </MultiTrigger.Conditions>
                <Setter TargetName="PART_ResizeGrip" Property="Visibility" Value="Visible" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    
    <Style x:Key="Style.BasicWindow" TargetType="{x:Type local:BasicWindow}">
        <Setter Property="Controls.Common.Extensions:WindowExtensions.OverridesDefaultBehavior" Value="True"/>
        <Setter Property="AllowsTransparency" Value="True" /> 
        <Setter Property="ButtonsPanel" Value="{StaticResource ItemsPanelTemplate.Window.Buttons}"/>
        <Setter Property="ButtonStyle" Value="{DynamicResource Style.Window.Button}"/>
        <Setter Property="Foreground" Value="{DynamicResource Brush.Text}" />
        <Setter Property="IconTemplate" Value="{StaticResource DataTemplate.Window.Icon}"/>
        <Setter Property="ResizeGripTemplate" Value="{StaticResource DataTemplate.Window.ResizeGrip}" />
        <Setter Property="ShowInTaskbar" Value="True" />
        <Setter Property="TextOptions.TextFormattingMode" Value="Display" />
        <Setter Property="TitleTemplate" Value="{StaticResource DataTemplate.Window.Title}" />
        <Setter Property="UseLayoutRounding" Value="True" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome
                    CaptionHeight="35"
                    CornerRadius="0"
                    GlassFrameThickness="0,0,0,1"
                    ResizeBorderThickness="5"/>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{x:Type local:BasicWindow}" TargetType="{x:Type local:BasicWindow}" BasedOn="{StaticResource Style.BasicWindow}">
        <Style.Triggers>
            <Trigger Property="Type" Value="Window">
                <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
                <Setter Property="Template" Value="{StaticResource Template.BasicWindow}"/>
            </Trigger>
            <Trigger Property="Type" Value="Tool">
                <Setter Property="Template" Value="{StaticResource Template.BasicWindow}"/>
            </Trigger>
            <Trigger Property="Type" Value="Blank">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type local:BasicWindow}">
                            <Border
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                ClipToBounds="True"
                                WindowChrome.IsHitTestVisibleInChrome="True"
                                Padding="{TemplateBinding Padding}">
                                <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="WindowChrome.WindowChrome">
                    <Setter.Value>
                        <WindowChrome
                            CaptionHeight="0"
                            CornerRadius="0"
                            GlassFrameThickness="0"
                            ResizeBorderThickness="0"/>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- ............................................... Parts -->

    <Style x:Key="{x:Type local:WindowFooter}" TargetType="{x:Type local:WindowFooter}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:WindowFooter">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Padding="{TemplateBinding Padding}">
                        <ContentPresenter TextElement.Foreground="{TemplateBinding Foreground}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
